//
//  MyOrdersViewController.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/21.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "MyOrdersViewController.h"

#import "OrderColumnView.h"

#import "ProductOrdersView.h"

@interface MyOrdersViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong) OrderColumnView *columnView;//栏目框

@property (nonatomic,strong) UIScrollView *scrollView;

@property (nonatomic,strong) ProductOrdersView *allOrdersV;
@property (nonatomic,strong) ProductOrdersView *paymentOrdersV;
@property (nonatomic,strong) ProductOrdersView *sendOrdersV;
@property (nonatomic,strong) ProductOrdersView *receiveOrdersV;
@property (nonatomic,strong) ProductOrdersView *evaluationOrdersV;
@property (nonatomic,strong) ProductOrdersView *salesAfterOrdersV;

@end

@implementation MyOrdersViewController

//进入我的订单传的来源
- (void)setSourceType:(NSInteger)sourceType{
    _sourceType = sourceType;
    self.columnView.sourceType = _sourceType;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.scrollView setContentOffset:CGPointMake(ScreenWidth*_sourceType, 0) animated:NO];
    });
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topTitle = @"我的订单";
    self.topNavView.backgroundColor = MainBackColor;
    self.view.backgroundColor = MainBackColor;
    
    [self initUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshClick:) name:@"refreshOrderListNotification" object:nil];
    
}
#pragma mark - noti
-(void)refreshClick:(NSNotification *)noti{
    NSDictionary *dict = (NSDictionary *)noti.userInfo;
    NSInteger listType = [dict[@"listType"] integerValue];
    [self.columnView setSourceType:listType];
    [self.scrollView setContentOffset:CGPointMake(ScreenWidth*listType, 0) animated:YES];
    switch (listType) {
        case 0:
            [self.allOrdersV getNewData];
            break;
        case 1:
            [self.paymentOrdersV getNewData];
            break;
        case 2:
            [self.sendOrdersV getNewData];
            break;
        case 3:
            [self.receiveOrdersV getNewData];
            break;
        case 4:
            [self.evaluationOrdersV getNewData];
            break;
        case 5:
            [self.salesAfterOrdersV getNewData];
            break;
        default:
            break;
    }
}
#pragma mark - UI
-(void)initUI{
    [self.view addSubview:self.columnView];
    [self.view addSubview:self.scrollView];
    
    for (int i = 0; i<6; i++) {
        ProductOrdersView *orderV = [[ProductOrdersView alloc] initWithFrame:CGRectMake(i*ScreenWidth, 0, ScreenWidth, self.scrollView.height) withSourceType:i];
        [self.scrollView addSubview:orderV];
        if (i == 0) {
            self.allOrdersV = orderV;
        }else if (i == 1) {
            self.paymentOrdersV = orderV;
        }else if (i == 2) {
            self.sendOrdersV = orderV;
        }else if (i == 3) {
            self.receiveOrdersV = orderV;
        }else if (i == 4) {
            self.evaluationOrdersV = orderV;
        }else if (i == 5) {
            self.salesAfterOrdersV = orderV;
        }
    }
    WEAKSELF(self);
    [self.columnView setTouchBlock:^(NSInteger index) {
        [weakSelf.scrollView setContentOffset:CGPointMake(index * ScreenWidth, 0)];
    }];
}

#pragma mark - lazy
- (OrderColumnView *)columnView{
    if (!_columnView) {
        _columnView = [[OrderColumnView alloc] initWithFrame:CGRectMake(0, self.topNavView.bottom, ScreenWidth, 56) withTitles:@[@"全部",@"待付款",@"待发货",@"待收货",@"待评价",@"售后"]];
    }
    return _columnView;
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.columnView.bottom, ScreenWidth, ScreenHeight - kHeight_NavBar - self.columnView.height - kSafe_Bottom)];
        _scrollView.backgroundColor = MainBackColor;
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.contentSize = CGSizeMake(ScreenWidth*6, _scrollView.height);
        _scrollView.delegate = self;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x/ScreenWidth;
    [self.columnView setSourceType:index];
}


@end
